package dbu.jdbc;

import dbu.jdbc.facade.JdbcResultSetMetaDataImpl;

public final class JdbcResultSetMetaData extends JdbcResultSetMetaDataImpl {

	public static final String SEMICOL = ";";
	public static final String COMMA = ",";
	private String meta;

	// TODO not public
	public JdbcResultSetMetaData(String meta) {
		this.meta = meta;
	}

	/** Returns the number of columns in this ResultSet object. */
	@Override
	public int getColumnCount() {
		return getTokens().length;
	}

	/** Get the designated column's name. */
	@Override
	public String getColumnName(int index) {
		return getTokens()[index].trim();
	}

	@Override
	public String toString() {
		return meta;
	}

	private String[] getTokens() {
		String result = meta.trim();
		if (result.indexOf(SEMICOL) >= 0) {
			result = result.substring(result.indexOf(SEMICOL) + SEMICOL.length());
		}
		return result.split(COMMA);
	}

	public int getColumnIndex(String column) {
		for(int index = 0; index < getColumnCount(); index++) {
			if (column.equalsIgnoreCase(getColumnName(index))) {
				return index;
			}
		}
		return -1;
	}
}
